|
In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. This is typically accomplished by augmenting a variable's accessor method (or property definition) to check for a previously-created instance. If none exists a new instance is created, placed into the variable, and this new object is returned to the caller in a just-in-time fashion. In this manner object creation is deferred until first use which can, in some circumstances (e.g., sporadic object access), increase system responsiveness and speed startup by bypassing large-scale object pre-allocation. (Note that this may have attendant counter-effects on overall performance, however, as the impact of object instantiation is then amortized across the startup/warm-up phase of the system.) In multithreaded code, access to lazy-initialized objects/state must be synchronized to guard against a race condition. See lazy evaluation for a general treatment of this idea. In heavily imperative languages this pattern carries hidden dangers, as does any programming habit that relies on shared state. ==The "lazy factory"== In a software design pattern view, lazy initialization is often used together with a factory method pattern. This combines three ideas: * Using a factory method to get instances of a class (factory method pattern) * Store the instances in a map, so you get the ''same'' instance the next time you ask for an instance with ''same'' parameter (multiton pattern) * Using lazy initialization to instantiate the object the first time it is requested (lazy initialization pattern) 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Lazy initialization」の詳細全文を読む スポンサード リンク
|